<!-- 使用 type="home" 属性设置首页，其他页面不需要设置，默认为page；推荐使用json5，更强大，且允许注释 -->
<route lang="json5">
{
  style: {
    navigationBarTitleText: '销售出库',
    custom: true,
    // 'app-plus': {
    //   titleNView: {
    //     buttons: [
    //       {
    //         type: 'menu',
    //         color: '#000',
    //       },
    //     ],
    //   },
    // },
  },
}
</route>
<template>
  <Loyout class="outorder-item-page" bgc="#F3F8FE">
    <view class="outorder-item-box">
      <wd-form ref="form" :model="outFormdata">
        <wd-cell-group border>
          <wd-input
            type="text"
            v-model="outFormdata.out_order_no"
            label="单号"
            prop="out_order_no"
            suffix-icon="list"
            readonly
          />
          <wd-picker
            :ellipsis="true"
            :columns="lstOrg"
            readonly
            label="往来单位"
            v-model="outFormdata.org_code"
          />
          <wd-picker
            :columns="Lst_order_goods_type"
            label="模式"
            readonly
            v-model="outFormdata.goods_type"
          />
          <wd-input
            @click="toDetail"
            type="text"
            v-model="count"
            label="商品数量"
            suffix-icon="search"
            readonly
          />
        </wd-cell-group>
      </wd-form>
    </view>
    <!-- <template #footer>
      <wd-button class="flex1 ml-20" size="large" @click="startScan">开始扫描</wd-button>
    </template> -->
  </Loyout>
</template>

<script lang="ts" setup>
import Loyout from '@/layouts/index.vue'
import { useOrderConfigStore } from '@/store/orderconfig'
import { Lst_order_goods_type, GoodsType, OrderType, OutOrderStatus } from '@/enum'
import OutSqlLite, { OutorderTable } from '@/utils/out-sqlite'
import OutGoodsSqlLite, { OutorderGoodsTable } from '@/utils/out-goods-sqlite'
import useScan from '@/hooks/useScan'
import { useToast } from 'wot-design-uni'

const Toast = useToast()
const orderConfigStore = useOrderConfigStore()

const count = ref(0)

let outFormdata = reactive<OutorderTable>({
  out_order_no: '',
  org_code: '',
  goods_type: GoodsType.NORMAL,
  order_type: OrderType.OUT,
  create_at: '',
  status: OutOrderStatus.UNEXPORT,
  delete_flag: '0',
})

const outGoodsFormdata = reactive<OutorderGoodsTable>({
  out_order_no: '',
  org_code: '',
  qrcode: '',
  create_at: '',
  delete_flag: '0',
})

const lstOrg = [
  {
    value: '1',
    label: '测试组织1',
  },
  {
    value: '2',
    label: '测试组织2',
  },
]

const orderConfig = computed(() => orderConfigStore.orderConfig)

onLoad(async (options) => {
  if (options.out_order_no) {
    const data = await OutSqlLite.search({ out_order_no: options.out_order_no })
    console.log('data', data)
    outFormdata = Object.assign(outFormdata, data[0])
    outGoodsFormdata.out_order_no = outFormdata.out_order_no
    outGoodsFormdata.org_code = outFormdata.org_code
    console.log('outFormdata', outFormdata)
    console.log('outGoodsFormdata', outGoodsFormdata)
  }
})

onShow(async (options) => {
  getOutItemCounts()
})

useScan(async (code) => {
  try {
    console.log('data', code)
    outGoodsFormdata.qrcode = code
    Toast.success('扫描成功')
    await OutGoodsSqlLite.insert([outGoodsFormdata])
    getOutItemCounts()
  } catch (error) {
    console.log('error', error)
    Toast.error('扫描失败')
  }
})

const getOutItemCounts = async () => {
  const counts = await OutGoodsSqlLite.countSql({ out_order_no: outFormdata.out_order_no })
  count.value = counts
}

const toDetail = () => {
  uni.navigateTo({
    url: '/pages/outorder/out-detail?out_order_no=' + outFormdata.out_order_no,
  })
}
</script>
